// This source is subject to the Microsoft Public License.
// See http://www.microsoft.com/resources/sharedsource/licensingbasics/sharedsourcelicenses.mspx.
// All other rights reserved.

using System;
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Web.UI.WebControls;
using System.Windows.Forms;
using System.Xml;

using QuoteAdvantageEditor.CrmSdk;


namespace QuoteAdvantageEditor
{
	/// <summary>
	/// Dialog for adding rules to parent products.
	/// </summary>
	public class LinkedItemListDlg : System.Windows.Forms.Form
	{
      private AuthoringEngine eng = null;
      protected XmlNode objInputItemNode = null;
      protected XmlNode objOutputRuleNode = null;
      protected Organization objOrg = null;
     
      private System.Windows.Forms.Button btnOK;
      private System.Windows.Forms.ListView ctlProductList;
      private System.Windows.Forms.ColumnHeader colName;
      private System.Windows.Forms.ColumnHeader colProductId;
      private System.Windows.Forms.Button btnAddLinkedItem;
      private System.Windows.Forms.ColumnHeader colUom;
      private System.Windows.Forms.Button btnModify;
      private System.Windows.Forms.Button btnDelete;
		/// <summary>
		/// Required designer variable.
		/// </summary>
		private System.ComponentModel.Container components = null;
      
      public AuthoringEngine Engine
      {
         get { return eng; }
         set { eng = value; }
      }

      public XmlNode InputItemNode
      {
         get { return objInputItemNode; }
         set { objInputItemNode = value; }
      }

      public XmlNode OutputRuleNode
      {
         get { return objOutputRuleNode; }
         set { objOutputRuleNode = value; }
      }

		public LinkedItemListDlg()
		{
			//
			// Required for Windows Form Designer support
			//
			InitializeComponent();

			//
			// TODO: Add any constructor code after InitializeComponent call
			//
		}

		/// <summary>
		/// Clean up any resources being used.
		/// </summary>
		protected override void Dispose( bool disposing )
		{
			if( disposing )
			{
				if(components != null)
				{
					components.Dispose();
				}
			}
			base.Dispose( disposing );
		}

		#region Windows Form Designer generated code
		/// <summary>
		/// Required method for Designer support - do not modify
		/// the contents of this method with the code editor.
		/// </summary>
		private void InitializeComponent()
		{
         this.btnOK = new System.Windows.Forms.Button();
         this.ctlProductList = new System.Windows.Forms.ListView();
         this.colName = new System.Windows.Forms.ColumnHeader();
         this.colProductId = new System.Windows.Forms.ColumnHeader();
         this.btnAddLinkedItem = new System.Windows.Forms.Button();
         this.colUom = new System.Windows.Forms.ColumnHeader();
         this.btnModify = new System.Windows.Forms.Button();
         this.btnDelete = new System.Windows.Forms.Button();
         this.SuspendLayout();
         // 
         // btnOK
         // 
         this.btnOK.Location = new System.Drawing.Point(420, 252);
         this.btnOK.Name = "btnOK";
         this.btnOK.Size = new System.Drawing.Size(80, 23);
         this.btnOK.TabIndex = 18;
         this.btnOK.Text = "Close";
         this.btnOK.Click += new System.EventHandler(this.btnOK_Click);
         // 
         // ctlProductList
         // 
         this.ctlProductList.Anchor = ((System.Windows.Forms.AnchorStyles)((((System.Windows.Forms.AnchorStyles.Top | System.Windows.Forms.AnchorStyles.Bottom) 
            | System.Windows.Forms.AnchorStyles.Left) 
            | System.Windows.Forms.AnchorStyles.Right)));
         this.ctlProductList.Columns.AddRange(new System.Windows.Forms.ColumnHeader[] {
                                                                                         this.colName,
                                                                                         this.colProductId,
                                                                                         this.colUom});
         this.ctlProductList.FullRowSelect = true;
         this.ctlProductList.HideSelection = false;
         this.ctlProductList.Location = new System.Drawing.Point(12, 20);
         this.ctlProductList.MultiSelect = false;
         this.ctlProductList.Name = "ctlProductList";
         this.ctlProductList.Size = new System.Drawing.Size(488, 220);
         this.ctlProductList.TabIndex = 20;
         this.ctlProductList.View = System.Windows.Forms.View.Details;
         this.ctlProductList.Click += new System.EventHandler(this.ctlProductList_Click);
         this.ctlProductList.DoubleClick += new System.EventHandler(this.ctlProductList_DoubleClick);
         this.ctlProductList.SelectedIndexChanged += new System.EventHandler(this.ctlProductList_SelectedIndexChanged);
         // 
         // colName
         // 
         this.colName.Text = "Name";
         this.colName.Width = 253;
         // 
         // colProductId
         // 
         this.colProductId.Text = "Product ID";
         this.colProductId.Width = 108;
         // 
         // btnAddLinkedItem
         // 
         this.btnAddLinkedItem.Location = new System.Drawing.Point(12, 252);
         this.btnAddLinkedItem.Name = "btnAddLinkedItem";
         this.btnAddLinkedItem.Size = new System.Drawing.Size(120, 23);
         this.btnAddLinkedItem.TabIndex = 21;
         this.btnAddLinkedItem.Text = "Add Linked Item";
         this.btnAddLinkedItem.Click += new System.EventHandler(this.btnAddLinkedItem_Click);
         // 
         // colUom
         // 
         this.colUom.Text = "Unit of Measure";
         this.colUom.Width = 90;
         // 
         // btnModify
         // 
         this.btnModify.Location = new System.Drawing.Point(144, 252);
         this.btnModify.Name = "btnModify";
         this.btnModify.Size = new System.Drawing.Size(80, 23);
         this.btnModify.TabIndex = 22;
         this.btnModify.Text = "Modify";
         this.btnModify.Click += new System.EventHandler(this.btnModify_Click);
         // 
         // btnDelete
         // 
         this.btnDelete.Location = new System.Drawing.Point(232, 252);
         this.btnDelete.Name = "btnDelete";
         this.btnDelete.Size = new System.Drawing.Size(80, 23);
         this.btnDelete.TabIndex = 23;
         this.btnDelete.Text = "Delete";
         this.btnDelete.Click += new System.EventHandler(this.btnDelete_Click);
         // 
         // LinkedItemListDlg
         // 
         this.AcceptButton = this.btnOK;
         this.AutoScaleBaseSize = new System.Drawing.Size(5, 14);
         this.ClientSize = new System.Drawing.Size(510, 288);
         this.Controls.Add(this.btnDelete);
         this.Controls.Add(this.btnModify);
         this.Controls.Add(this.btnAddLinkedItem);
         this.Controls.Add(this.ctlProductList);
         this.Controls.Add(this.btnOK);
         this.Font = new System.Drawing.Font("Tahoma", 8.25F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((System.Byte)(0)));
         this.FormBorderStyle = System.Windows.Forms.FormBorderStyle.FixedDialog;
         this.MaximizeBox = false;
         this.MinimizeBox = false;
         this.Name = "LinkedItemListDlg";
         this.ShowInTaskbar = false;
         this.StartPosition = System.Windows.Forms.FormStartPosition.CenterParent;
         this.Text = "Linked Items";
         this.Load += new System.EventHandler(this.LinkedItemListDlg_Load);
         this.ResumeLayout(false);

      }
		#endregion
      
      private void LinkedItemListDlg_Load(object sender, System.EventArgs e)
      {
         Cursor oldCursor = Cursor.Current;
         Cursor.Current = Cursors.WaitCursor; 

         objOrg = eng.LoadOrganization();
         RefreshList();

         Cursor.Current = oldCursor;
      }    
     

      private void btnOK_Click(object sender, System.EventArgs e)
      {
         this.DialogResult = DialogResult.OK;
         this.Close();
      }

      private void btnCancel_Click(object sender, System.EventArgs e)
      {
         this.Close();
      }           


      private void RefreshList()
      {
         ctlProductList.Items.Clear();

         // Load existing linked items
         foreach (XmlNode objLinkedItem in objInputItemNode.ChildNodes)
         {
            string strProductNumber = objLinkedItem.InnerText;
            string strUomId = objLinkedItem.Attributes["uom"].Value;
            Product objProduct = eng.LoadProductFromProductNumber(strProductNumber);
  
            ListViewItem   objItem = null;

            if (objProduct == null)
            {
               objItem = new ListViewItem("Product Description Not Available", 1);
               objItem.Tag = objLinkedItem;
               objItem.SubItems.Add(strProductNumber);
            }
            else
            {
               string strUomDescr = "[Default]";

               if (strUomId != "")
               {
                  strUomDescr = eng.LoadUnitOfMeasure(new Guid(strUomId), objOrg);
               }

               objItem = new ListViewItem(objProduct.Name, 1);
               objItem.Tag = objLinkedItem;
               objItem.SubItems.Add(objProduct.ProductNumber);
               objItem.SubItems.Add(strUomDescr);
            }

            ctlProductList.Items.Add(objItem); 
         }

         if (ctlProductList.Items.Count > 0)
         {
            ctlProductList.Items[ctlProductList.Items.Count-1].Selected = true;
         }

         EnableControls();
      }

      private void EnableControls()
      {
         if (ctlProductList.SelectedItems.Count == 0)
         {
            btnModify.Enabled = false;
            btnDelete.Enabled = false;
         }
         else
         {
            btnModify.Enabled = true;
            btnDelete.Enabled = true;
         }
      }

      private void ctlProductList_Click(object sender, System.EventArgs e)
      {
         EnableControls();
      }

      private void ctlProductList_DoubleClick(object sender, System.EventArgs e)
      {
         ModifyItem();
      }

      private void ModifyItem()
      {
         ParentLinkedItemDlg dlg = new ParentLinkedItemDlg();
         XmlNode  node = (XmlNode)ctlProductList.SelectedItems[0].Tag;

         dlg.Engine = eng;
         dlg.InputRuleNode = node;

         if (DialogResult.OK == dlg.ShowDialog())
         {
            node.ParentNode.ReplaceChild(dlg.OutputRuleNode, node);
         }
         RefreshList();
      }

      private void DeleteItem()
      {
         XmlNode  node = (XmlNode)ctlProductList.SelectedItems[0].Tag;

         node.ParentNode.RemoveChild(node);
         RefreshList();
      }

      private void btnModify_Click(object sender, System.EventArgs e)
      {
         ModifyItem();
      }

      private void btnDelete_Click(object sender, System.EventArgs e)
      {
         DeleteItem();
      }

      private void btnAddLinkedItem_Click(object sender, System.EventArgs e)
      {
         XmlDocument objDocument = objInputItemNode.OwnerDocument;
         XmlNode objNewRuleNode = objDocument.CreateElement("", "LinkedItem", "");
         objInputItemNode.AppendChild(objNewRuleNode);
                  
         ParentLinkedItemDlg dlg = new ParentLinkedItemDlg();
         
         dlg.InputRuleNode = objNewRuleNode;
         dlg.Engine = eng;
         dlg.ShowDialog(this);
         
         if (dlg.DialogResult == DialogResult.OK)
         {
            objInputItemNode.ReplaceChild(dlg.OutputRuleNode, objNewRuleNode);
         }
         else
         {
            objInputItemNode.RemoveChild(objNewRuleNode);
         }

         RefreshList();
      }

      private void ctlProductList_SelectedIndexChanged(object sender, System.EventArgs e)
      {
         EnableControls();
      }


	} // end class
}
